package great.util;

import org.springframework.util.CollectionUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validation;
import java.util.Set;

/**
 * 手动调用api方法校验对象
 * @author zhangshun
 */
public class MyValidationUtils {

	public static <T> void validate(@Valid T user) {

		Set<ConstraintViolation<@Valid T>> validateSet = Validation.buildDefaultValidatorFactory()
			.getValidator()
			.validate(user);

		if (!CollectionUtils.isEmpty(validateSet)) {
			String messages = validateSet.stream()
				.map(ConstraintViolation::getMessage)
				.reduce((m1, m2) -> m1 + "；" + m2)
				.orElse("参数输入有误！");
			throw new IllegalArgumentException(messages);

		}

	}

}